package com.example.cloud.service.statistics.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.system.User;
import com.example.cloud.service.statistics.StatisticsReasonService;
import com.example.cloud.utils.RequestUtil;
import com.example.cloud.utils.ResultUtil;
import com.example.cloud.utils.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/5/22.
 */
@Service
public class StatisticsReasonServiceImpl implements StatisticsReasonService {

    @PersistenceContext
    EntityManager em;

    @Autowired
    private SQLManager sqlManager;

    @Override
    public Result doSearch1(Map params) {
        User u = RequestUtil.getLoginUser();
        JSONArray ja = new JSONArray();
        StringBuilder sql = new StringBuilder("select user_name as userName,COUNT(user_name) as count from complaint_record");
        sql.append(" where account_id = " + u.getAccountId());
        if(!params.isEmpty()){
            if(params.containsKey("startTime") && params.get("startTime") != null && StringUtils.isNotBlank(params.get("startTime").toString())){
                sql.append(" and creation_time >= '" + params.get("startTime").toString() + "'");
            }
            if(params.containsKey("endTime") && params.get("endTime") != null && StringUtils.isNotBlank(params.get("endTime").toString())){
                sql.append(" and creation_time <= '" + params.get("endTime").toString() + "'");
            }
        }
        sql.append(" GROUP BY user_name");
        List<Map> mapList = sqlManager.execute(new SQLReady(sql.toString()), Map.class);
        for (Map map : mapList) {
            JSONObject jo = new JSONObject();
            jo.put("name", map.get("userName"));
            jo.put("count", map.get("count"));
            ja.add(jo);
        }
        return ResultUtil.success(ja);
    }

    @Override
    public Result doSearch2(Map params) {
        User u = RequestUtil.getLoginUser();
        JSONArray ja = new JSONArray();
        StringBuilder sql = new StringBuilder("select reason_name as reasonName,COUNT(reason_name) as count from complaint_record");
        sql.append(" where account_id = " + u.getAccountId());
        if(!params.isEmpty()){
            if(params.containsKey("startTime") && params.get("startTime") != null && StringUtils.isNotBlank(params.get("startTime").toString())){
                sql.append(" and creation_time >= '" + params.get("startTime").toString() + "'");
            }
            if(params.containsKey("endTime") && params.get("endTime") != null && StringUtils.isNotBlank(params.get("endTime").toString())){
                sql.append(" and creation_time <= '" + params.get("endTime").toString() + "'");
            }
        }
        sql.append(" GROUP BY reason_name");
        List<Map> mapList = sqlManager.execute(new SQLReady(sql.toString()), Map.class);
        for (Map map : mapList) {
            JSONObject jo = new JSONObject();
            jo.put("name", map.get("reasonName"));
            jo.put("count", map.get("count"));
            ja.add(jo);
        }
        return ResultUtil.success(ja);
    }
}
